﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RPGProject.GameWorld;
using RPGProject.GameWorld.GameObjects;

namespace RPGProject.LoopSystem
{
    class EnemyLoop : Loop
    {
        private int AP;
        private Enemy enemy;
        public EnemyLoop(LoopManager loopManager, Enemy enemy)
            : base(loopManager, 100)
        {
            this.enemy = enemy;
        }
        public override FuncType RunOnce()
        {
            if ((int)this.enemy["hp"] <= 0)
                return FuncType.REMOVE;
            if (this.AP == 0)
            {
                this.AP = 3;
                this.enemy.Listen(new GameObjectEvent("enemyreset"));
            }
            GameObjectEvent turn = new GameObjectEvent("enemyturn");
            if (AP == 1)
                turn["last"] = true;
            else
                turn["last"] = false;
            GameObjectEvent temp = this.enemy.Listen(turn);
            if (temp != null && temp.Name == "projectile")
            {
                Projectile shuriken = (Projectile)temp["source"];
                this.loopManager.AddFunc(FuncType.PRIORITY_TO_FRONT, new ProjectileLooper(shuriken, this.loopManager));
            }
            this.AP--;
            if (this.AP == 0)
                return FuncType.ADD_TO_END;
            else
                return FuncType.REPEAT;
        }
        public override string ToString()
        {
            return this.enemy.ToString();
        }
    }
}
